﻿//======================================================================
//
//        Copyright (C) 2008 HOHO``
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :作业中心路径操作类,继承于作业中心类.
//
//        created by 裘中杰 at  $time$
//======================================================================

using System;
using System.Data;
using System.Data.Linq;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.Security;
using System.Xml.Linq;
using Hisun.Models;


namespace Hisun.Controllers
{
    public class AbcCostCenterFramework : AbcCostCenter
    {

        private List<int> costCenterIDs;

        public List<int> CostCenterIDs
        {
            get { return costCenterIDs; }
            set { costCenterIDs = value; }
        }


        public AbcCostCenterFramework()
        {
            //costCenterIDs = new List<int>();
        }



        /// <summary>
        /// 重写 内部嵌套
        /// </summary>
        /// <param name="menuID"></param>
        /// <param name="totalMenus"></param>
        /// <param name="tn"></param>
        override protected void FillData2TreeView(int menuID, IEnumerable<TABC_JS_CostCenter> totalMenus, ref TreeNode tn)
        {
            var menus = (from m in totalMenus
                         where m.上级作业中心ID == menuID
                         orderby m.作业中心ID ascending
                         select m);
            if (menus.Count() == 0)
                return;
            foreach (var menu in menus)
            {
                if (menu.作业中心ID == menu.上级作业中心ID)
                    throw new Exception(string.Format("严重错误 ！作业中心ID中条目[{0}]重复与上级作业中心ID！", menu.作业中心名称));
                TreeNode childTN = new TreeNode(menu.作业中心名称, menu.作业中心ID.ToString(), @"\Skins\Default\Menu\folder.gif");
                if (costCenterIDs.Contains(menu.作业中心ID))
                    childTN.Checked = true;
                if (menu.停用.GetValueOrDefault(true))
                    childTN.Text = string.Format(@"<font color=""red"">{0}</font>", childTN.Text);
                FillData2TreeView(menu.作业中心ID, totalMenus, ref childTN);
                tn.ChildNodes.Add(childTN);
            }
        }

        /// <summary>
        /// 初始化 读取指定路径包含的所有作业中心ID
        /// </summary>
        /// <param name="costCenterID"></param>
        /// <returns></returns>
        public int InitPathCostCenterIDsByCostCenterID(int costCenterID)
        {
            //costCenterIDs = GetPathCostCenterIDsByCostCenterID(costCenterID);
            //if (costCenterIDs == null)
            //    costCenterIDs = new List<int>();
            return 0;
        }


        /// <summary>
        /// 获得指定路径包含的所有作业中心ID并返回
        /// </summary>
        /// <param name="costCenterID"></param>
        /// <returns></returns>
        //public List<int> GetPathCostCenterIDsByCostCenterID(int costCenterID)
        //{
        //    //if (db.TABC_JS_CostCenterPathFramework.Count(c => c.作业中心ID == costCenterID) == 0)
        //    //    return null;
        //    //var costIDs = (from c in db.TABC_JS_CostCenterPathFramework
        //    //               where c.作业中心ID == costCenterID
        //    //               select c.目标中心ID).ToList<int>();
        //   // return costIDs;
        //    return ;
 
        //}


        /// <summary>
        /// 设置作业中心一对多路径,有事务处理
        /// </summary>
        /// <param name="costCenterID">起始作业中心ID</param>
        /// <param name="costCenterPathIDs">多个作业中心ID</param>
        /// <returns></returns>
        public int SetPath(int costCenterID, string companyID, string accountSuiteID, int costDriverID, List<int> costCenterPathIDs)
        {
            //if (costCenterID == 0)
            //    throw new Exception("没有指定作业中心ID");
            //IEnumerable<TABC_JS_CostCenterPathFramework> deleteNeeded = (from c in db.TABC_JS_CostCenterPathFramework
            //                    where c.作业中心ID == costCenterID
            //                    select c);

            ////事务处理代码阶段
            
            //if (db.Connection.State != ConnectionState.Open)
            //{
            //    db.Connection.Open();
            //}
            

            //System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction();
            //db.Transaction = tran;
            //try
            //{

            //    db.TABC_JS_CostCenterPathFramework.DeleteAllOnSubmit(deleteNeeded);

            //    foreach (var costCenterPathID in costCenterPathIDs)
            //    {
            //        TABC_JS_CostCenterPathFramework ccpf = new TABC_JS_CostCenterPathFramework();
            //        ccpf.公司ID = companyID;
            //        ccpf.账套ID = accountSuiteID;
            //        ccpf.成本动因ID = costDriverID;
            //        ccpf.作业中心ID = costCenterID;
            //        ccpf.目标中心ID = costCenterPathID;
            //        ccpf.录入人 = HttpContext.Current.Session[ServerVars.sysSessionUserName].ToString();
            //        db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(ccpf);
            //    }
            //    db.SubmitChanges();
            //    tran.Commit();
            //}
            //catch (Exception ex)
            //{
            //    lastError = ex.Message;
            //    tran.Rollback();
            //    return -1;
            //}
            //return 0;
            return 0;
        }


        /// <summary>
        /// 设置作业中心一对多路径,有事务处理
        /// </summary>
        /// <param name="costCenterID">起始作业中心ID</param>
        /// <param name="tnc">页面TreeNode集合</param>
        /// <returns></returns>
        public int SetPath(int costCenterID, string companyID, string accountSuiteID, int costDriverID, TreeNodeCollection tnc)
        {
            if (tnc.Count == 0)
            {
                lastError = "没有元素可以更新!";
                return -1;
            }
            List<int> costCenterPathIDs = new List<int>();
            foreach (TreeNode tn in tnc)
            {
                costCenterPathIDs.Add(Convert.ToInt32(tn.Value));
            }
            return SetPath(costCenterID, companyID, accountSuiteID, costDriverID, costCenterPathIDs);
        }

    }

}
